package ar.edu.uba.fi.jmit.monitorengine.backend.client.view;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ar.edu.uba.fi.jmit.monitorengine.backend.shared.dto.StatisticPair;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ReceiverStatsPanel extends VerticalPanel {
	
	private Panel statsPanel;
	
	private Map<String, StatisticPanel> existingPanels;
	
	public ReceiverStatsPanel() {
		super();
		this.setSize("800px", "100%");
		this.add(new HTML("<h1>Receiver Statistics</h1>"));
		statsPanel = new VerticalPanel();
		statsPanel.setWidth("100%");
		statsPanel.setStyleName("stat-panel");
		this.add(statsPanel);
		existingPanels = new HashMap<String, StatisticPanel>();
	}
	
	public void showStatistics(List<StatisticPair> statistics) {
		for(StatisticPair stat:statistics) {
			this.addStatistic(stat.getKey(), stat.getValue());
		}
	}

	private void addStatistic(String key, String value) {
		StatisticPanel panel = existingPanels.get(key);
		if(panel == null) {
			panel = new StatisticPanel(key, value);
			existingPanels.put(key, panel);
			statsPanel.add(panel);
		} else {
			panel.setValue(value);
		}
		
	}
	
	private class StatisticPanel extends HorizontalPanel {
		
		private Label labelKey;
		
		private Label labelValue;
		
		StatisticPanel(String key, String value) {
			super();
			labelKey = new Label(key + ": ");
			labelKey.setStyleName("stat");
			this.add(labelKey);
			labelValue = new Label(value);
			labelValue.setStyleName("stat-value");
			this.add(labelValue);
			this.setStyleName("stat-panel");
		}
		
		void setValue(String newValue) {
			labelValue.setText(newValue);
		}
	}
}
